package com.pureut.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.production.domain.*;
import com.pureut.production.domain.dto.ClothInspectionDto;
import com.pureut.production.domain.dto.ClothInspectionPdaDto;
import com.pureut.production.domain.dto.SelectCombinationListDto;
import com.pureut.production.domain.vo.ClothInspectionPdaVo;
import com.pureut.production.domain.vo.ClothInspectionVo;
import com.pureut.production.domain.vo.SelectCombinationListVo;
import com.pureut.production.mapper.*;
import com.pureut.production.service.ClothInspectionDetailService;
import com.pureut.production.service.ClothInspectionService;
import com.pureut.production.service.CutOrdersDocumentDetailService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 验布实现层
 * Author:C
 * Date:2023/02/20 10:28
 * @DESC
 */
@Service
public class ClothInspectionServiceImpl extends ServiceImpl<ClothInspectionMapper, ClothInspection> implements ClothInspectionService {

    @Resource
    ClothInspectionMapper clothInspectionMapper;

    @Resource
    CutOrdersDocumentDetailMapper cutOrdersDocumentDetailMapper;

    @Resource
    CutOrdersDocumentMapper cutOrdersDocumentMapper;

    @Resource
    SpunlaceOutputDetailMapper spunlaceOutputDetailMapper;

    @Resource
    ClothInspectionDetailService clothInspectionDetailService;

    @Resource
    ClothInspectionDetailMapper clothInspectionDetailMapper;

    @Resource
    CutOrdersDocumentDetailService cutOrdersDocumentDetailService;


    /**
     * 验布列表
     *
     * @param clothInspectionVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<ClothInspectionDto> getList(ClothInspectionVo clothInspectionVo) {

        //验布状态
        List<SysDictData> clothStatusArray = DictUtils.getDictCache("cloth_inspection_status");
        Map<String, String> clothStatusMap = clothStatusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<ClothInspectionDto> list = clothInspectionMapper.getList(clothInspectionVo);

        for (ClothInspectionDto entity : list) {
            //验布状态
            entity.setClothInspectionStatusDict(clothStatusMap.get(entity.getClothInspectionStatus()));
        }
        return list;
    }

    /**
     * 验布
     *
     * @param clothInspectionPdaVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClothInspectionPdaDto clothInspection(ClothInspectionPdaVo clothInspectionPdaVo) {
        ClothInspectionPdaDto clothInspectionPdaDto = new ClothInspectionPdaDto();
        //开始验布逻辑
        if (clothInspectionPdaVo.getTabType() == 1) {
            //获取分切制令单明细数据状态
            CutOrdersDocumentDetail documentDetail = cutOrdersDocumentDetailMapper.selectOne(new QueryWrapper<CutOrdersDocumentDetail>().lambda().eq(CutOrdersDocumentDetail::getMasterVolumeNum, clothInspectionPdaVo.getInputValue()));
            if (documentDetail == null) {
                clothInspectionPdaDto.setResultMessage("未查询到此母卷，请核实!");
                clothInspectionPdaDto.setResultCode("500");
                return clothInspectionPdaDto;
            }
            //校验该母卷是否验布完成
            if (documentDetail.getClothInspectionStatus() == 4) {
                clothInspectionPdaDto.setResultMessage("此母卷已完成验布,不能进行开始验布");
                clothInspectionPdaDto.setResultCode("500");
                return clothInspectionPdaDto;
            }
            //校验是不是首次扫描
            ClothInspection clothInspection = clothInspectionMapper.selectOne(new QueryWrapper<ClothInspection>().lambda().eq(ClothInspection::getMasterVolumeNumber, clothInspectionPdaVo.getInputValue()));
            if (clothInspection == null) {
                //为首次扫描
                clothInspectionPdaDto.setMasterVolumeNumber(clothInspectionPdaVo.getInputValue());
                clothInspectionPdaDto.setResultMessage("开始验布,开始长度:" + 0);
                //获取分切制令单数据
                CutOrdersDocument cutOrdersDocument = cutOrdersDocumentMapper.selectOne(new QueryWrapper<CutOrdersDocument>().lambda().eq(CutOrdersDocument::getSplittingOrderNumber, documentDetail.getDocumentNum()));
                //保存数据验布
                ClothInspection inspection = new ClothInspection();
                //获取水刺制令单号
                SpunlaceOutputDetail spunlaceOutputDetail = spunlaceOutputDetailMapper.selectOne(new QueryWrapper<SpunlaceOutputDetail>().lambda().eq(SpunlaceOutputDetail::getProductionNumber, documentDetail.getProductNumber()));
                inspection.setMasterVolumeNumber(clothInspectionPdaVo.getInputValue())
                        .setClothInspectionStatus(1)
                        .setProductNumber(documentDetail.getProductNumber())
                        .setMaterialCode(documentDetail.getMaterialCode())
                        .setSpinesMakeOrders(spunlaceOutputDetail.getSpunlaceOrderNumber())
                        .setSplittingOrderNumber(documentDetail.getDocumentNum())
                        .setLineTypeName(cutOrdersDocument.getLineTypeName())
                        .setCreateTime(new Date())
                        .setDeptId(SecurityUtils.getDeptId());
                save(inspection);
                //保存验布明细数据
                ClothInspectionDetail clothInspectionDetail = new ClothInspectionDetail();
                clothInspectionDetail.setClothInspectionBy(SecurityUtils.getUsername())
                        .setMasterVolumeNumber(clothInspectionPdaVo.getInputValue())
                        .setStartLength("0")
                        .setStartTime(new Date());
                clothInspectionDetailService.save(clothInspectionDetail);
                clothInspectionPdaDto.setResultCode("200");
                return clothInspectionPdaDto;
            } else {
                //根据母卷号码获取明细中最新的数据
                List<ClothInspectionDetail> clothInspectionDetail = clothInspectionDetailMapper.selectList(new QueryWrapper<ClothInspectionDetail>().lambda().eq(ClothInspectionDetail::getMasterVolumeNumber, clothInspection.getMasterVolumeNumber()).orderByDesc(ClothInspectionDetail::getStartTime));
                ClothInspectionDetail inspectionDetail = clothInspectionDetail.get(0);
                //判断是否存在结束长度
                if (inspectionDetail.getEndLength() == null || "0".equals(inspectionDetail.getEndLength())) {
                    clothInspectionPdaDto.setResultCode("500");
                    clothInspectionPdaDto.setResultMessage("上一验布人未完成验布");
                } else {
                    clothInspectionPdaDto.setMasterVolumeNumber(clothInspectionPdaVo.getInputValue());
                    clothInspectionPdaDto.setResultMessage("开始验布,开始长度:" + inspectionDetail.getEndLength());
                    clothInspectionPdaDto.setResultCode("200");
                    //保存验布明细数据
                    ClothInspectionDetail inspectionDetail1 = new ClothInspectionDetail();
                    inspectionDetail1.setClothInspectionBy(SecurityUtils.getUsername())
                            .setStartLength(inspectionDetail.getEndLength())
                            .setMasterVolumeNumber(clothInspectionPdaVo.getInputValue())
                            .setStartTime(new Date());
                    boolean save = clothInspectionDetailService.save(inspectionDetail1);
                    System.out.println(save);
                }
                return clothInspectionPdaDto;
            }
        }
        //部分验布逻辑
        if (clothInspectionPdaVo.getTabType() == 2) {
            //第一步
            if (clothInspectionPdaVo.getStep() == 1) {
                //判断此母卷是否已经开始验布
                List<ClothInspectionDetail> clothInspectionDetail = clothInspectionDetailMapper.selectList(new QueryWrapper<ClothInspectionDetail>().lambda().eq(ClothInspectionDetail::getMasterVolumeNumber, clothInspectionPdaVo.getInputValue()).orderByDesc(ClothInspectionDetail::getStartTime));
                ClothInspectionDetail clothInspectionDetail1 = clothInspectionDetail.get(0);
                if (clothInspectionDetail1.getEndLength() != null) {
                    clothInspectionPdaDto.setResultMessage("此母卷未开始验布");
                    clothInspectionPdaDto.setResultCode("500");
                    clothInspectionPdaDto.setMasterVolumeNumber(clothInspectionPdaVo.getInputValue());
                } else {
                    ClothInspectionDetail inspectionDetail = clothInspectionDetail.get(0);
                    //校验验布人是否为当前操作人
                    if (inspectionDetail.getClothInspectionBy().equals(SecurityUtils.getUsername())) {
                        clothInspectionPdaDto.setResultCode("200");
                        clothInspectionPdaDto.setResultMessage("");
                    } else {
                        clothInspectionPdaDto.setResultCode("500");
                        clothInspectionPdaDto.setResultMessage("上一验布人未完成验布");
                    }
                }
                return clothInspectionPdaDto;
            }
            //第二步
            if (clothInspectionPdaVo.getStep() == 2) {
                //修改对应明细
                List<ClothInspectionDetail> clothInspectionDetail = clothInspectionDetailMapper.selectList(new QueryWrapper<ClothInspectionDetail>().lambda().eq(ClothInspectionDetail::getMasterVolumeNumber, clothInspectionPdaVo.getMasterVolumeNumber()).orderByDesc(ClothInspectionDetail::getStartTime));
                ClothInspectionDetail inspectionDetail = clothInspectionDetail.get(0);
                inspectionDetail.setEndTime(new Date())
                        .setEndLength(clothInspectionPdaVo.getInputValue());
//                        .setClothInspectionTime(getDateHour(inspectionDetail.getStartTime(), new Date()));
                clothInspectionDetailService.updateById(inspectionDetail);
                clothInspectionPdaDto.setResultCode("200");
                clothInspectionPdaDto.setMasterVolumeNumber(inspectionDetail.getMasterVolumeNumber());
                clothInspectionPdaDto.setResultMessage("部分验布完成,结束长度:" + clothInspectionPdaVo.getInputValue());

                //修改上方数据状态
                ClothInspection inspection = clothInspectionMapper.selectOne(new QueryWrapper<ClothInspection>().lambda().eq(ClothInspection::getMasterVolumeNumber, clothInspectionPdaVo.getMasterVolumeNumber()));
                inspection.setClothInspectionStatus(2);
                updateById(inspection);
                //修改分切制令单数据状态
                CutOrdersDocumentDetail documentDetail = cutOrdersDocumentDetailMapper.selectOne(new QueryWrapper<CutOrdersDocumentDetail>().lambda().eq(CutOrdersDocumentDetail::getMasterVolumeNum, clothInspectionPdaVo.getMasterVolumeNumber()));
                documentDetail.setClothInspectionStatus(3);
                cutOrdersDocumentDetailService.updateById(documentDetail);

                return clothInspectionPdaDto;
            }
        }
        //验布完成逻辑
        if (clothInspectionPdaVo.getTabType() == 3) {
            //第一步
            if (clothInspectionPdaVo.getStep() == 1) {
                //判断此母卷是否已经开始验布
                List<ClothInspectionDetail> clothInspectionDetail = clothInspectionDetailMapper.selectList(new QueryWrapper<ClothInspectionDetail>().lambda().eq(ClothInspectionDetail::getMasterVolumeNumber, clothInspectionPdaVo.getInputValue()).orderByDesc(ClothInspectionDetail::getStartTime));
                ClothInspectionDetail inspectionDetail = clothInspectionDetail.get(0);
                if (inspectionDetail.getEndLength() != null) {
                    clothInspectionPdaDto.setResultMessage("此母卷未开始验布");
                    clothInspectionPdaDto.setResultCode("500");
                    clothInspectionPdaDto.setMasterVolumeNumber(clothInspectionPdaVo.getInputValue());
                } else {

                    //校验验布人是否为当前操作人
                    if (inspectionDetail.getClothInspectionBy().equals(SecurityUtils.getUsername())) {
                        clothInspectionPdaDto.setResultCode("200");
                        clothInspectionPdaDto.setResultMessage("");
                    } else {
                        clothInspectionPdaDto.setResultCode("500");
                        clothInspectionPdaDto.setResultMessage("上一验布人未完成验布");
                    }
                }
                return clothInspectionPdaDto;
            }
            //第二步
            if (clothInspectionPdaVo.getStep() == 2) {
                //修改对应明细
                List<ClothInspectionDetail> clothInspectionDetail = clothInspectionDetailMapper.selectList(new QueryWrapper<ClothInspectionDetail>().lambda().eq(ClothInspectionDetail::getMasterVolumeNumber, clothInspectionPdaVo.getMasterVolumeNumber()).orderByDesc(ClothInspectionDetail::getStartTime));
                ClothInspectionDetail inspectionDetail = clothInspectionDetail.get(0);
                inspectionDetail.setEndTime(new Date())
                        .setEndLength(clothInspectionPdaVo.getInputValue());
//                        .setClothInspectionTime(getDateHour(inspectionDetail.getStartTime(), new Date()));
                clothInspectionDetailService.updateById(inspectionDetail);
                clothInspectionPdaDto.setResultCode("200");
                clothInspectionPdaDto.setMasterVolumeNumber(inspectionDetail.getMasterVolumeNumber());
                clothInspectionPdaDto.setResultMessage("验布完成,结束长度:" + clothInspectionPdaVo.getInputValue());

                //修改上方数据状态
                ClothInspection inspection = clothInspectionMapper.selectOne(new QueryWrapper<ClothInspection>().lambda().eq(ClothInspection::getMasterVolumeNumber, clothInspectionPdaVo.getMasterVolumeNumber()));
                inspection.setClothInspectionStatus(3);
                updateById(inspection);

                //修改分切制令单数据状态
                CutOrdersDocumentDetail documentDetail = cutOrdersDocumentDetailMapper.selectOne(new QueryWrapper<CutOrdersDocumentDetail>().lambda().eq(CutOrdersDocumentDetail::getMasterVolumeNum, clothInspectionPdaVo.getMasterVolumeNumber()));
                documentDetail.setClothInspectionStatus(4);
                cutOrdersDocumentDetailService.updateById(documentDetail);

                return clothInspectionPdaDto;
            }
        }
        return clothInspectionPdaDto;
    }


    /**
     * 获取两个时间之间相差多少小时
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public String getDateHour(Date startTime, Date endTime) {
        long time = startTime.getTime();
        long time1 = endTime.getTime();
        long a = (time1 - time) / 1000 / 60;
        double b = a/60.0;
        return String.valueOf(b);
    }
}
